<?php
class Cfd_model extends CI_Model
{
    private $ci,$redis;
    public function __construct(){
        $this->load->database();
        $this->ci = & get_instance();
        //链接redis
        $this->ci->load->driver('cache');
        $this->ci->cache->redis->is_supported();
        $this->redis = $this->ci->cache->redis;
    }

    public function getPosition($data,$limit='all',$orderby=''){
        $this->db->select()->from("cfd_cfd_position")->where($data);
        if($limit!='all'){
             $limit = abs($limit);
             $this->db->limit($limit);
        }
        if($orderby!=''){
            $this->db->order_by($orderby);
        }
        $query =  $this->db->get();
        if($limit==1) return $query->row_array();
        return $query->result_array();
    }

    public function getTradeLog($data,$limit='all',$orderby=''){
        $this->db->select()->from("cfd_cfd_trade_log")->where($data);
        if($limit!='all'){
             $limit = abs($limit);
             $this->db->limit($limit);
        }
        if($orderby!=''){
            $this->db->order_by($orderby);
        }
        $query =  $this->db->get();
        if($limit==1) return $query->row_array();
        return $query->result_array();
    }

	public function getTradeTotalStatistics($data){
        $this->db->select('sum(num*times) as cash')->from("cfd_cfd_trade_log")->where($data['where']);
		
		if(!empty($data['source_in'])){
			$this->db->where_in('source',$data['source_in']);
		}

        $query =  $this->db->get();
        return $query->row_array();
    }

	public function getTradeTotalYk($data,$sdate="",$edate=''){
        $this->db->select('sum(yk) as yk')->from("cfd_cfd_trade_log")->where($data['where']);
		
		if(!empty($data['source_in'])){
			$this->db->where_in('source',$data['source_in']);
		}

		if(!empty($sdate)){
			$this->db->where('date >=', $sdate);
		}

		if(!empty($edate)){
			$this->db->where('date <=', $edate);
		}

        $query =  $this->db->get();
        return $query->row_array();
    }

	public function getTradeStatistics($data,$sdate="",$edate=''){
        $this->db->select('(num*times) as cash,date,pre_open_comm,num,uid,dtype,yk')->from("cfd_cfd_trade_log");

		if(!empty($data['where'])){
			$this->db->where($data['where']);
		}
		
		if(!empty($data['source_in'])){
			$this->db->where_in('source',$data['source_in']);
		}

		if(!empty($sdate)){
			$this->db->where('date >=', $sdate);
		}

		if(!empty($edate)){
			$this->db->where('date <=', $edate);
		}
		
		$this->db->order_by('date DESC,id DESC');

        $query =  $this->db->get();
        return $query->result_array();
    }

	public function getTradeYk($data,$sdate="",$edate=''){
        $this->db->select('yk,date,uid')->from("cfd_cfd_trade_log")->where($data['where']);
		
		if(!empty($data['source_in'])){
			$this->db->where_in('source',$data['source_in']);
		}

		if(!empty($sdate)){
			$this->db->where('date >=', $sdate);
		}

		if(!empty($edate)){
			$this->db->where('date <=', $edate);
		}
		
		$this->db->order_by('date DESC,id DESC');

        $query =  $this->db->get();
        return $query->result_array();
    }

	public function getHistoryYk($uid,$dtype=""){
		$where	=	"where kp='close'";
		$where	.=   !empty($uid) ?" and uid ='$uid'":"";
		$where	.=   !empty($dtype) ?" and dtype ='$dtype'":"";

		$sql	=	"select sum(yk) as history_yk from cfd_cfd_trade_log ".$where;
		$query	=	$this->db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['history_yk'])) {
			return del0($row['history_yk']);
		}

        return 0;
    }

}